const Web3 = require('web3')
const options = {
    defaultAccount: '0x4Dff49A45E35C2970A549Ba56D90cEb64E6B37D2',
    defaultBlock: 'latest',
    transactionConfirmationBlocks: 1,
}

export const web3 = new Web3(Web3.givenProvider || 'ws://localhost:7545', null, options);


var chapter_ctr = require('@/contracts/Chapter.json')
var head_ctr = require('@/contracts/Head.json')
var storyIndex_ctr = require('@/contracts/StoryIndex.json')

var CTR_chapter = new web3.eth.Contract(chapter_ctr.abi)
var CTR_Head = new web3.eth.Contract(head_ctr.abi)
var CTR_StroyIndex = new web3.eth.Contract(storyIndex_ctr.abi, "0x772b5887B59bef4ba6C7547c47092F22FdBa1524")

export function deployChapter({auther, owner, prev, text, star=0,type}){
    console.log([auther, owner, prev, text, star,type])
    return new Promise((resolve,reject)=>{
        CTR_chapter.deploy({
            data:chapter_ctr.bytecode,
            arguments: [auther, owner, prev, text, star,type]
        }).send({
            from: owner,
            gas: 1500000,
            gasPrice: '30000000000000'
        })
        .on('error', (err) => { 
            console.log("deployChapter err:", err); 
        })
        .on('transactionHash', (transactionHash) => { 
            console.log(transactionHash)
        })
        .on('receipt', (receipt) => {
            console.log("contractAddress", receipt.contractAddress) // contains the new contract address
        })
        .on('confirmation', (confirmationNumber, receipt) => {
            console.log("deployChapter confirmation ---", receipt)
            resolve(receipt.contractAddress)
        })
        .then((ins)=>{
            console.log("deployChapter------>", ins)
            resolve(ins.address)
        }).catch((error)=>{
            console.log("deployChapter", error)
            reject(error)
        })
    })
}
export function chapterIndexToHead({address,prev,h_addr,level=1}){
    console.log("@@@@@@@>>", address,prev,level)
    let CTR_Head = new web3.eth.Contract(head_ctr.abi, h_addr)
    return new Promise((resolve,reject)=>{
        CTR_Head.methods.addNode(address,prev,level).send({
            gas: 1500000,
            gasPrice: '30000000000000'
        }).then((flag) => {
            console.log("chapterIndexToHead", flag)
            resolve(flag)
        }).catch((err)=>{
            reject(err)
        })
    })
}
export function addChapter({prev,text,star=0,owner,h_addr,level,type}){
    return new Promise((resolve,reject)=>{
        deployChapter({auther:owner,owner,prev,text,star,type}).then(async (chapterAddress)=>{
            await chapterIndexToHead({address:chapterAddress,prev,h_addr,level})
            resolve()
        }).catch((err)=>{
            reject(err)
        })
    })
}
export function storyList(){
    return new Promise((resolve,reject)=>{
        CTR_StroyIndex.methods.list().call().then((list) => {
            console.log("list->", list)
            resolve(list)
        }).catch((err)=>{
            reject(err)
        })
    })
}
export function storyTest(){
    return new Promise((resolve,reject)=>{
        CTR_StroyIndex.methods.test2().call().then((res) => {
            console.log("storyTest->", res)
            resolve(res)
        }).catch((err)=>{
            reject(err)
        })
    })
}
export async function addStoryToIndex({address}){
    await CTR_StroyIndex.methods.append(address).send({
        // from: '0xd42F15E027aE2F86bD05De4357fB282c561115c3',`
        gas: 1500000,
        gasPrice: '30000000000000'
    }).then((res)=>{
        console.log("addStoryToIndex", res)
    })
}
export function deployStoryHead({title, brief}){
    let address = ''
    // const gas = await CTR_Head.deploy({
    //     data: head_ctr.bytecode,
    //     arguments: [title, brief]
    // }).estimateGas();
    return new Promise((resolve,reject)=>{
        CTR_Head.deploy({
            data: head_ctr.bytecode,
            arguments: [title, brief]
        }, (error, transactionHash) => { console.log("error", error, transactionHash) }).send({
            gas: 1500000,
            gasPrice: '30000000000000'
        })
        .on('error', (err) => { 
            console.log(err); 
            reject(err)
        })
        .on('transactionHash', (transactionHash) => { 
            console.log(transactionHash)
        })
        .on('receipt', (receipt) => {
            console.log("contractAddress CTR_Head", receipt.contractAddress) // contains the new contract address
        })
        .on('confirmation', (confirmationNumber, receipt) => {
            console.log("CTR_Head.deploy confirmation",confirmationNumber, receipt)
            address = receipt.contractAddress
            resolve(receipt.contractAddress)
        })
        .then((ins)=>{
            console.log("deploy success", ins)
            // address = ins.address
            resolve(ins.address)
        })
    })
    
    
}
//获取章节的目录
export function getChapterIndex(h_addr){
    console.log("view h_addr", h_addr)
    let CTR_Head = new web3.eth.Contract(head_ctr.abi, h_addr)
    return new Promise((resolve,reject)=>{
        CTR_Head.methods.chapterIndex().call().then((list)=>{
            resolve(list)
        }).catch((err)=>{
            reject(err)
        })
    })
    
}
//获取章节的详情
export function getChapterDetail(addr){
    let CTR_chapter = new web3.eth.Contract(chapter_ctr.abi, addr)
    return new Promise((resolve,reject)=>{
        CTR_chapter.methods.detail().call().then((data)=>{
            resolve(data)
        }).catch((err)=>{
            reject(err)
        })

    })
}
//获取故事简介详情
export function storyDetail(addr){
    return new Promise((resolve,reject)=>{
        let CTR_Head = new web3.eth.Contract(head_ctr.abi, addr)
        CTR_Head.methods.detail().call().then((data)=>{
            resolve(data)
        }).catch((err)=>{
            console.log(err,"hhhhhhhhhhhh")
            reject(err)
        })
    })
}